package com.woniuxy01;

import java.io.*;
import java.util.Arrays;

/**
 * @author 作者
 * @create 2025-11-12 10:07
 */
public class Test01 {
    public static void main(String[] args) throws IOException {
        OutputStreamWriter osw=
                new OutputStreamWriter(new FileOutputStream("b.txt"),"gbk");
        //一次写一个字符或者字符串
        osw.write('a');
        osw.write('中');
        osw.write("bcd");
        osw.write("我们");
        //字符流入一段数据以后，调用flush刷新，循环多次写入数据时，每次循环结束调用一次刷新
        osw.flush();
        osw.close();

        InputStreamReader isr=
                new InputStreamReader(new FileInputStream("b.txt"),"gbk");
        //一次读取一个字符
        int mychar;
        while ((mychar=isr.read())!=-1){
            System.out.println((char)mychar);
        }
        isr.close();
    }

    private static void test05() throws IOException {
        InputStreamReader isr=
                new InputStreamReader(new FileInputStream("a.txt"));
        //一次读取一个字符数组
        int length;
        char[] chars=new char[3];
        while((length=isr.read(chars))!=-1){
            System.out.println(new String(chars));
        }
        isr.close();


    }

    private static void test04() throws IOException {
        InputStreamReader isr=
                new InputStreamReader(new FileInputStream("a.txt"));
        //一次读取一个字符
        int mychar;
        while ((mychar=isr.read())!=-1){
            System.out.println((char)mychar);
        }
        isr.close();
    }

    private static void test03() throws IOException {
        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("a.txt"));
        //一次写一个字符或者字符串
        osw.write('a');
        osw.write('中');
        osw.write("bcd");
        osw.write("我们");
        //字符流入一段数据以后，调用flush刷新，循环多次写入数据时，每次循环结束调用一次刷新
        osw.flush();
        osw.close();
    }

    private static void test02() throws UnsupportedEncodingException {
        String str="abc中文";
        //编码
        byte[] bytes = str.getBytes("utf-8");
        //解码
        String str1 = new String(bytes,"utf-8");
        System.out.println(str1);
    }

    private static void test01() throws UnsupportedEncodingException {
        String str="abc中文";
        //将字符串转换成对应的编码，无参方法，使用的是当前环境的编码utf-8
        byte[] bytes = str.getBytes();
        System.out.println(Arrays.toString(bytes));
        System.out.println(Arrays.toString(str.getBytes("utf-8")));
        System.out.println(Arrays.toString(str.getBytes("gbk")));
    }
}
